#include "stdafx.h"
#include <QDomDocument>
#include <QFile>
#include <iostream>
#include "ConfigureData.h"


shared_ptr<ConfigureData> ConfigureData::GetInstance()
{
	if (!m_pConfigureData.get())
	{
		m_pConfigureData = shared_ptr<ConfigureData>(new ConfigureData());
		m_pConfigureData->LoadCfg("Init.xml");
	}
	return m_pConfigureData;
}

shared_ptr<ConfigureData> ConfigureData::m_pConfigureData;

bool ConfigureData::LoadCfg(QString strFileName)
{
	QFile file(strFileName);
	if (!file.open(QFile::ReadOnly | QFile::Text)) {
		std::cerr << "Error: Cannot read file " << ToString(strFileName)
			<< ": " << ToString(file.errorString())
			<< std::endl;
		return false;
	}

	QString errorStr;
	int errorLine;
	int errorColumn;

	QDomDocument doc;
	if (!doc.setContent(&file, false, &errorStr, &errorLine, &errorColumn))
	{
		std::cerr << "Error: Parse error at line " << errorLine << ", "
			<< "column " << errorColumn << ": "
			<< qPrintable(errorStr) << std::endl;
		return false;
	}
	QDomElement root = doc.documentElement();
	if (root.tagName() != "param")
	{
		std::cerr << "Error: Not a param file" << std::endl;
		return false;
	}

	QDomNode child = root.firstChild();
	while (!child.isNull())
	{
		QDomElement ele = child.toElement();
		if (ele.tagName() == "FaceMoveParam")
		{
			m_faceParam.m_fLeftAngle = ele.attribute("LeftAngle").toFloat();
			m_faceParam.m_fRightAngle = ele.attribute("RightAngle").toFloat();
			m_faceParam.m_fTolTime = ele.attribute("TolTime").toFloat();
			m_faceParam.m_fStartPresentRight = ele.attribute("PerFromRight").toFloat();
			m_faceParam.m_fStartPresentLeft = ele.attribute("PerFromLeft").toFloat();
		}
		else if (ele.tagName() == "FaceAddMin")
		{
			m_iFaceAddMin =  ele.attribute("value").toFloat();
		}
		else if (ele.tagName() == "FaceIndex")
		{
			m_vFaceInx.clear();
			QString strData = ele.attribute("value");
			QStringList strList = strData.split(',');
			for (QString str: strList)
			{
				m_vFaceInx.push_back(str.toInt());
			}
		}
		else if (ele.tagName() == "EvaluateParam")
		{
			m_evlParam.m_fRadius = ele.attribute("radius").toFloat()*0.001;
			m_evlParam.m_anglePlus = ele.attribute("anglePlus").toFloat();
			m_evlParam.m_delHeight = ele.attribute("height").toFloat()*0.001;
		}
		child = child.nextSibling();
	}
	return true;
}